 /**
 * Description: 生成业务表控制器
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-18 下午 16:03:50
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-18   xk       0.0.1     Initial
 */
package com.metro.generator.gen.controller;

 import com.github.pagehelper.PageInfo;
 import com.metro.core.annotation.BusinessDesc;
 import com.metro.core.page.Page;
 import com.metro.core.vo.JsonResult;
 import com.metro.core.vo.QueryParams;
 import com.metro.core.vo.ResultCode;
 import com.metro.core.vo.inf.IResult;
 import com.metro.paperless.common.beans.constant.DicTypeEnum;
 import com.metro.paperless.common.utils.DicUtil;
 import com.metro.generator.gen.beans.vo.GenTable;
 import com.metro.generator.gen.service.inf.IGenTableService;
 import com.metro.generator.gen.util.GenDicTypeHelper;
 import com.metro.paperless.sys.beans.vo.SysAttachment;
 import com.metro.paperless.web.controller.BaseController;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.servlet.view.RedirectView;

 import javax.servlet.http.HttpServletRequest;
 import java.util.HashMap;
 import java.util.Map;

 /**
 * 生成业务表控制器<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-18]
 */
@Controller
@RequestMapping("/gentable")
public class GenTableController extends BaseController {
    //菜单编码
    private static final String menuCode = "gentable";
    //模块描述
    private static final String MODULE_DESC = "生成业务表";
    @Autowired
    private IGenTableService genTableService;
    @Override
    protected String getViewDir() {
        return "gen/";
    }


     /**
      * 查询表及字段信息
      *
      * @param genTable 生成业务表页面表单对象
      * @param page 分页配置
      * @return 结果视图
      * @author xk
      * @since [0.0.1]
      * @version [0.0.1, 2018-01-18]
      */
     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询表及字段信息")
     @SuppressWarnings("all")
     @RequestMapping(value = "getTableInfoFromDbSchema", method = {RequestMethod.GET,RequestMethod.POST})
     @ResponseBody
     public JsonResult getTableInfoFromDbSchema(String tableName) {
         JsonResult jr = new JsonResult();
         try {
             GenTable entity = genTableService.queryTableColumnFromDbSchema(tableName);
             Map<String, Object> map = new HashMap<>();
             map.put("table", entity);
             map.put("compareTypes", DicUtil.getLabelValueList(DicTypeEnum.QUERY_COMPARE_TYPE));
             map.put("formTypes", DicUtil.getLabelValueList(DicTypeEnum.FORM_TYPE));
             map.put("dicTypes", GenDicTypeHelper.getAllDictionaryType());
             jr.setData(map);
             jr.setResult(ResultCode.SUCCESS);
         } catch (Exception ex){
             log.error("查询表及字段信息",ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }

     /**
      * 查询表及字段信息
      *
      * @param genTable 生成业务表页面表单对象
      * @param page 分页配置
      * @return 结果视图
      * @author xk
      * @since [0.0.1]
      * @version [0.0.1, 2018-01-18]
      */
     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询表及字段信息")
     @SuppressWarnings("all")
     @RequestMapping(value = "getTableInfoForEdit", method = {RequestMethod.GET,RequestMethod.POST})
     @ResponseBody
     public JsonResult getTableInfoForEdit(Long tableId) {
         JsonResult jr = new JsonResult();
         try {
             GenTable entity = genTableService.getById(tableId);
             Map<String, Object> map = new HashMap<>();
             map.put("table", entity);
             map.put("compareTypes", DicUtil.getLabelValueList(DicTypeEnum.QUERY_COMPARE_TYPE));
             map.put("formTypes", DicUtil.getLabelValueList(DicTypeEnum.FORM_TYPE));
             map.put("dicTypes", GenDicTypeHelper.getAllDictionaryType());
             jr.setData(map);
             jr.setResult(ResultCode.SUCCESS);
         } catch (Exception ex){
             log.error("查询表及字段信息",ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }

     @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "生成代码")
     @SuppressWarnings("all")
     @RequestMapping(value = "genCode", method = {RequestMethod.GET,RequestMethod.POST})
     @ResponseBody
     public JsonResult genCode(String ids) {
         JsonResult jr = new JsonResult();
         try {
             SysAttachment file = genTableService.genCode(ids);
             jr.setData(file);
             jr.setResult(ResultCode.SUCCESS);
         } catch (Exception ex){
             log.error("查询表及字段信息",ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }

    /**
     * 去查询生成业务表
     * 
     * @param genTable 生成业务表页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询生成业务表列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "list", method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView list(GenTable genTable) {
        ModelAndView mav = new ModelAndView();
        try {
            mav.addObject("genProjects", DicUtil.getLabelValueList(DicTypeEnum.GEN_PROJECT));
            mav.setViewName(getView("gentable_list"));
            mav.addObject("searchEntity", genTable);
        } catch (Exception ex) {
            log.error("查询生成业务表出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }

    /**
     * 查询生成业务表
     *
     * @param genTable 生成业务表页面表单对象
     * @param page 分页配置
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "查询生成业务表列表")
    @RequiresPermissions(menuCode + ":view")
    @SuppressWarnings("all")
    @RequestMapping(value = "dolist", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JsonResult dolist(GenTable genTable, Page page, QueryParams<GenTable> queryParams) {
        JsonResult jr = new JsonResult();
        try {
            queryParams.setPaging(page);
            queryParams.setEntity(genTable);
            PageInfo<GenTable> pageInfo = genTableService.queryForPage(queryParams);
            jr.setData(pageInfo);
            jr.setResult(ResultCode.SUCCESS);
        } catch (Exception ex){
            log.error("查询生成业务表出错",ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 去新增生成业务表
     * 
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去新增生成业务表")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "create")
    public ModelAndView createForm(Model model, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            GenTable entity = new GenTable();
            entity.setColumnNum(2);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav, "create");
        } catch (Exception ex) {
            log.error("去新增生成业务表出错", ex);
            mav.addObject("ex", ex);
            mav.setViewName("/commons/error/error");
        }
        return mav;
    }
    
    /**
     * 初始化新增修改生成业务表所需参数
     * @author xk
     */
    private void initCreateOrUpdateFrom(ModelAndView mav, String action){
        mav.addObject("genProjects", DicUtil.getLabelValueList(DicTypeEnum.GEN_PROJECT));
        mav.addObject("tables", genTableService.queryAllTableAsLabelValue());
        mav.addObject("action", action);
        mav.setViewName(getView("gentable_form"));
    }
    
    /**
     * 新增生成业务表
     * 
     * @param entity 生成业务表页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "新增生成业务表")
    @RequiresPermissions(menuCode + ":add")
    @RequestMapping(value = "save", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult saveGenTable(GenTable entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = genTableService.save(entity);
            jr.setResult(res);
            log.debug("新增生成业务表成功【{}】", entity);
        } catch (Exception ex) {
            log.error("新增生成业务表出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }

    /**
     * 删除生成业务表
     * 
     * @param id 生成业务表页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "删除生成业务表")
    @RequiresPermissions(menuCode + ":delete")
    @RequestMapping(value = "delete/{id}", method = { RequestMethod.DELETE })
    @ResponseBody
    public JsonResult delete(@PathVariable String id, HttpServletRequest request){
        JsonResult jr = new JsonResult();
        try {
            log.debug("删除生成业务表id={}",id);
            IResult codeMsg = genTableService.deleteByIds(id);
            jr.setResult(codeMsg);
        } catch (Exception ex) {
            log.error("删除生成业务表出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去修改生成业务表
     * 
     * @param id 生成业务表页面表单对象唯一标识
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去修改生成业务表")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "edit/{id}", method = { RequestMethod.GET})
    public ModelAndView updateForm(@PathVariable Long id, HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        try {
            GenTable entity = genTableService.getById(id);
            mav.addObject("entity", entity);
            initCreateOrUpdateFrom(mav,"edit");
        } catch (Exception ex) {
            log.error("去修改生成业务表", ex);
            mav.setView(new RedirectView("list"));
        }
        return mav;
    }

    /**
     * 修改生成业务表
     * 
     * @param entity 生成业务表页面表单对象
     * @param request 请求对象
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "修改生成业务表")
    @RequiresPermissions(menuCode + ":edit")
    @RequestMapping(value = "update", method = { RequestMethod.POST})
    @ResponseBody
    public JsonResult updateGenTable(GenTable entity, HttpServletRequest request) {
        JsonResult jr = new JsonResult();
        try {
            IResult res = genTableService.update(entity);
            jr.setResult(res);
            log.debug("修改生成业务表成功【{}】", entity);
        } catch (Exception ex) {
            log.error("修改生成业务表出错", ex);
            jr.setResult(ResultCode.ERROR);
        }
        return jr;
    }
    
    /**
     * 去查看生成业务表
     * 
     * @param id 生成业务表页面表单对象唯一标识
     * @return 结果视图
     * @author xk
     * @since [0.0.1]
     * @version [0.0.1, 2018-01-18]
     */
    @BusinessDesc(ModuleDesc = MODULE_DESC, MethodDesc = "去查看生成业务表详情")
    @RequiresPermissions(menuCode + ":view")
    @RequestMapping(value = "info/{id}", method = { RequestMethod.GET })
    public ModelAndView info(@PathVariable Long id) {
        ModelAndView mav= new ModelAndView();
        try {
            mav.addObject("entity", genTableService.getById(id));
            mav.setViewName(getView("gentable_info"));
        } catch (Exception ex) {
            log.error("查看生成业务表出错", ex);
        }
        return mav;
        
    }
    
}
